﻿using System;
using System.Drawing;

namespace ZYCommon
{
    

    public class PointBuffer
    {
        private int intPointCount = 0;
        private Point[] myPoints = new Point[0x10];

        public void AppendPoint(Point p)
        {
            lock (this)
            {
                if (this.intPointCount >= this.myPoints.Length)
                {
                    Point[] pointArray = new Point[(int) (this.myPoints.Length * 1.5)];
                    for (int i = 0; i < this.intPointCount; i++)
                    {
                        pointArray[i] = this.myPoints[i];
                    }
                    this.myPoints = pointArray;
                }
                this.myPoints[this.intPointCount] = p;
                this.intPointCount++;
            }
        }

        public void Clear()
        {
            lock (this)
            {
                this.intPointCount = 0;
                this.myPoints = new Point[0x10];
            }
        }

        public Rectangle GetBounds()
        {
            return GetBounds(this.ToPointArray());
        }

        public static Rectangle GetBounds(Point[] ps)
        {
            if ((ps != null) && (ps.Length > 1))
            {
                int x = ps[0].X;
                int num2 = ps[0].X;
                int y = ps[0].Y;
                int num4 = ps[0].Y;
                for (int i = 0; i < ps.Length; i++)
                {
                    if (x < ps[i].X)
                    {
                        x = ps[i].X;
                    }
                    if (num2 > ps[i].X)
                    {
                        num2 = ps[i].X;
                    }
                    if (y < ps[i].Y)
                    {
                        y = ps[i].Y;
                    }
                    if (num4 > ps[i].Y)
                    {
                        num4 = ps[i].Y;
                    }
                }
                return new Rectangle(num2, num4, x - num2, y - num4);
            }
            return Rectangle.Empty;
        }

        public static RectangleF GetBounds(PointF[] ps)
        {
            if ((ps != null) && (ps.Length > 1))
            {
                float XMax = ps[0].X;
                float XMin = ps[0].X;
                float YMax = ps[0].Y;
                float YMin = ps[0].Y;
                for (int iCount = 0; iCount < ps.Length; iCount++)
                {
                    if (XMax < ps[iCount].X)
                    {
                        XMax = ps[iCount].X;
                    }
                    if (XMin > ps[iCount].X)
                    {
                        XMin = ps[iCount].X;
                    }
                    if (YMax < ps[iCount].Y)
                    {
                        YMax = ps[iCount].Y;
                    }
                    if (YMin > ps[iCount].Y)
                    {
                        YMin = ps[iCount].Y;
                    }
                }
                return new RectangleF(XMin, YMin, XMax - XMin, YMax - YMin);
            }
            return RectangleF.Empty;
        }


        public Point[] ToClosedPointArray()
        {
            Point[] pointArray = null;
            if (this.intPointCount > 0)
            {
                lock (this)
                {
                    bool flag = this.myPoints[0] != this.myPoints[this.intPointCount - 1];
                    pointArray = new Point[flag ? (this.intPointCount + 1) : this.intPointCount];
                    for (int i = 0; i < this.intPointCount; i++)
                    {
                        pointArray[i] = this.myPoints[i];
                    }
                    if (flag)
                    {
                        pointArray[this.intPointCount] = this.myPoints[0];
                    }
                }
            }
            return pointArray;
        }

        public Point[] ToPointArray()
        {
            Point[] pointArray = null;
            if (this.intPointCount > 0)
            {
                lock (this)
                {
                    pointArray = new Point[this.intPointCount];
                    for (int i = 0; i < this.intPointCount; i++)
                    {
                        pointArray[i] = this.myPoints[i];
                    }
                }
            }
            return pointArray;
        }

        public int Count
        {
            get
            {
                return this.intPointCount;
            }
        }

        public Point LastPoint
        {
            get
            {
                if (this.intPointCount > 0)
                {
                    return this.myPoints[this.intPointCount - 1];
                }
                return Point.Empty;
            }
        }
    }
}

